package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.layout.SettingBar;
import com.qiantu.api.db.LLHouseDBManager;
import com.qiantu.api.entity.HouseBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.glide.GlideApp;

import java.util.List;

import okhttp3.Call;

/**
 * @Description: 绑定设备功能
 * @return:
 * @Author: qjw
 * @Time:  2022年12月20日
 */

public class BindingDevicesActivity extends AppActivity{

    private ImageView iconBindingDevice;
    private SettingBar itemHouseSetting;
    private TextView btnFinishBinding;
    private TextView tvBindingDeviceName;
    private TextView tvBindingDeviceId;
    private String devicesName;
    private String devicesImageUrl;
    private String codeNo;
    private String Code;
    private String houseSerialNo;
    private String house_name;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_binding_devices;
    }

    @Override
    protected void initView() {
        iconBindingDevice = (ImageView) findViewById(R.id.icon_binding_device);
        itemHouseSetting = (SettingBar) findViewById(R.id.item_house_setting);
        btnFinishBinding = (TextView) findViewById(R.id.btn_finish_binding);
        tvBindingDeviceName = (TextView) findViewById(R.id.tv_binding_device_name);
        tvBindingDeviceId = (TextView) findViewById(R.id.tv_binding_device_id);
        Intent intent = getIntent();
        devicesName = intent.getStringExtra("devicesName");
        devicesImageUrl = intent.getStringExtra("devicesImageUrl");
        codeNo = intent.getStringExtra("codeNo");
        Code = intent.getStringExtra("code");
        setOnClickListener(itemHouseSetting,btnFinishBinding);
        updateView();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        houseSerialNo = intent.getStringExtra("houseSerialNo");
        house_name = intent.getStringExtra("house_name");
    }

    @Override
    protected void onResume() {
        super.onResume();
        itemHouseSetting.setRightText(house_name);
        if (house_name==null || house_name.equals("")){
            List<HouseBean> houseall = LLHouseDBManager.getInstance(this).getAll();
            //要优化
            String name = houseall.get(0).getName();
            itemHouseSetting.setRightText(name);
            houseSerialNo = houseall.get(0).getHouseSerialNo();
        }
    }

    private void updateView() {
        GlideApp.with(this)
                .load(devicesImageUrl)
                .placeholder(new ColorDrawable(0x000000000))
                .error(new ColorDrawable(0x000000000)).into(iconBindingDevice);
        tvBindingDeviceName.setText(devicesName);
        tvBindingDeviceId.setText("ID:"+codeNo);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.item_house_setting:
                startActivity(new Intent(this,OwningHouseActivity.class));
                break;
            case R.id.btn_finish_binding:
                LLHttpManager.getDevicesBinding(this,houseSerialNo,Code,true,new HttpCallback<HttpData<Void>>(this){
                    @Override
                    public void onStart(Call call) {
                        super.onStart(call);
                    }

                    @Override
                    public void onSucceed(HttpData<Void> result) {
                        super.onSucceed(result);
                        if(result.getCode()==10000){
                            startActivity(new Intent(BindingDevicesActivity.this,HomeActivity.class));
                        }
                    }

                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);
                    }

                    @Override
                    public void onEnd(Call call) {
                        super.onEnd(call);
                    }
                });
                break;

        }
    }
}